display: flex or display: grid is applied.flex-direction (row or column).flex PropertyThe flex property is a shorthand for flex-grow, flex-shrink, and flex-basis.
flex: [grow] [shrink] [basis];
flex-grow: (Number) Dictates how much an item will grow relative to other items if there is extra space.flex-shrink: (Number) Dictates how much an item will shrink relative to other items if there isn't enough space.flex-basis: (Length/%) Defines the default size of an element before the remaining space is distributed.fr UnitThe fractional unit (fr) represents a fraction of the available space in the grid container. It makes responsive layouts simple.
Example: grid-template-columns: 1fr 2fr; creates two columns where the second is twice as wide as the first.
repeat(): A function to repeat track patterns. repeat(3, 1fr) is the same as 1fr 1fr 1fr.minmax(): Defines a size range. minmax(100px, 1fr) means the track must be at least 100px wide but can grow to fill the available space.| Property | Description | Common Values |
|---|---|---|
display | Defines the element as a flex container. | flex, inline-flex |
flex-direction | Sets the direction of the main axis. | row, row-reverse, column, column-reverse |
justify-content | Aligns items along the main axis. | flex-start, flex-end, center, space-between, space-around |
align-items | Aligns items along the cross axis. | stretch, flex-start, flex-end, center, baseline |
flex-wrap | Allows items to wrap onto multiple lines. | nowrap, wrap, wrap-reverse |
align-content | Aligns wrapped lines along the cross axis (only works with flex-wrap: wrap). | flex-start, center, space-between, stretch |
| Property | Description | Example Values |
|---|---|---|
display | Defines the element as a grid container. | grid, inline-grid |
grid-template-columns | Defines the number and sizes of columns. | 100px 1fr 2fr, repeat(3, 1fr) |
grid-template-rows | Defines the number and sizes of rows. | minmax(150px, auto) 1fr |
gap | Shorthand for setting row and column gaps. | 20px, 10px 20px (row column) |
justify-items | Aligns grid items along the row (inline) axis. | start, end, center, stretch |
align-items | Aligns grid items along the column (block) axis. | start, end, center, stretch |
Applied to the direct children of the flex container.
order: [integer]; (e.g., -1, 0, 1)flex: [grow] [shrink] [basis]; (e.g., flex: 1 1 auto;)align-self: [value]; (Overrides the container's align-items for a single item)Applied to the direct children of the grid container.
grid-column: [start-line] / [end-line]; (e.g., 1 / 3, span 2)grid-row: [start-line] / [end-line]; (e.g., 2 / 4)justify-self: [value]; (Overrides the container's justify-items for a single item)flex instead of the three individual properties. Use gap instead of row-gap and column-gap.flex-center could expand to:
display: flex;
justify-content: center;
align-items: center;
| Feature | Flexbox | Grid |
|---|---|---|
| Dimensions | One-dimensional (Row OR Column) | Two-dimensional (Rows AND Columns) |
| Primary Use | Content alignment, components | Overall page layout |
| Sizing Control | Content-driven (items grow/shrink) | Layout-driven (strict tracks) |
| Item Wrapping | Wraps as a new row/column | Places items in available cells, can leave gaps |
Grid lines are numbered starting from 1. You use these numbers to place items.
Line 1 Line 2 Line 3
+---------+---------+
| | | Line 1
+---------+---------+
| | | Line 2
+---------+---------+
<!-- HTML -->
<nav class="navbar">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
<!-- CSS -->
.navbar {
display: flex;
justify-content: center; /* Center items on the main axis */
align-items: center; /* Center items on the cross axis */
gap: 20px;
background-color: #333;
padding: 1rem;
}
<!-- HTML -->
<div class="gallery">
<div class="photo">1</div>
<div class="photo">2</div>
<div class="photo">3</div>
<div class="photo">4</div>
</div>
<!-- CSS -->
.gallery {
display: grid;
/* Create columns that are at least 200px wide, but grow to fill space */
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 10px;
}
flex-wrap: wrap; to the flex container.justify-content isn't working on my column layout.
justify-content always works on the main axis. For a column, this controls vertical alignment. Use align-items for horizontal alignment in a column layout.align-items: start; to the grid container.grid-column and grid-row.